package com.example.fastmybatis.controller;

import com.example.fastmybatis.entity.TUser;
import com.example.fastmybatis.service.TUserService;
import com.example.fastmybatis.util.CreateDataUtil;
import com.gitee.fastmybatis.core.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* @Description: 使用fastmybatis框架测试增删改差相关接口
* @Author:Mujiutian
* @CreateDate:2019/6/8 0:53
* @UpdateUser:
*/

@RestController
@RequestMapping(value = "/user/fastmybatis")
public class UserController {

    @Autowired
    @Qualifier("userService")
    TUserService userService;

    /**
    * @description:添加一个用户
    * @author:MuJiuTian
    * @createDate: 2019/6/8 14:24
    */
    @PostMapping("/addUser")
    public String addUser(){
        userService.addUser(CreateDataUtil.getUser());
        return "以fastmybatis方式"+"添加一个对象数据成功";
    }

    /**
    * @description:添加num个用户
    * @author:MuJiuTian
    * @createDate: 2019/6/8 14:24
    */
    @PostMapping("/addUsers")
    public String addUsers(int num){
        userService.addUsers(CreateDataUtil.getUsers(num));
        return "以fastmybatis方式"+"批量添加数据成功";
    }

    /**
    * @description:逻辑意义上删除用户,如果你想把数据库中的数据真正删除，那么修改实体类中，把@LogicDelete这个注解去掉
    * @author:MuJiuTian
    * @createDate: 2019/6/8 14:24
    */
    @DeleteMapping(value = "/deleteUser")
    public String deleteUser(int id){
        userService.deleteUser(id);
        return "以fastmybatis方式"+"删除成功";
    }

    /**
    * @description:更新用户数据,需要该用户所有数据
    * @author:MuJiuTian
    * @createDate: 2019/6/8 14:25
    */
    @PostMapping(value = "/updateUser")
    public String updateUser(TUser user){
        userService.updateUser(user);
        return "以fastmybatis方式"+"更新成功";
    }

    /**
    * @description:更新用户的某些属性字段，以Map传入
    * @author:MuJiuTian
    * @createDate: 2019/6/8 15:03
    */
    @PostMapping(value = "/updatePartUser")
    public String updatePartUser(int id){
        userService.updateUser(CreateDataUtil.getuserForMap(id));
        return "以fastmybatis方式"+"更新用户局部成功";
    }

    /**
    * @description:查找某个用户
    * @author:MuJiuTian
    * @createDate: 2019/6/8 14:25
    */
    @GetMapping(value = "/getUser")
    public String getUser(int id){
        TUser tUser = userService.getUser(id);
        return "以fastmybatis方式获取对象数据："+tUser.toString();
    }

    /**
    * @description:分页获取用户列表
    * @author:MuJiuTian
    * @createDate: 2019/6/8 14:25
    */
    @GetMapping(value = "/getPageUsers")
    public String getPageUsers(int pageIndex,int pageSize){
        Map<String,Object> map = userService.getPageResult(pageIndex,pageSize);
        return "以fastmybatis方式获取分页数据："+map.toString();
    }

    /**
    * @description:更详细更简单的获取分页用户数据
    * @author:MuJiuTian
    * @createDate: 2019/6/8 15:29
    */
    @GetMapping(value = "/getEasyPageUsers")
    public String getEasyPageUsers(int pageIndex,int pageSize){
        PageInfo<TUser> pageInfo = userService.getPageUsers(pageIndex,pageSize);
        return "获取更相信的数据成功";
    }

    /**
     * @description:以id排序获取用户
     * @author:MuJiuTian
     * @createDate: 2019/6/8 17:27
     */
    @GetMapping(value = "/getUsersForOrder")
    public String getUsersForOrder(){
        return userService.getUsersForOrder().toString();
    }
}
